package com.zhida.jiyangcenter.modle

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.tipFail
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIFamous
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.FamousDetailsEntity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import com.zhida.jiyangcenter.widget.ResizableImageView
import kotlinx.android.synthetic.main.activity_healthy_programme.*

@TitleBar(false)
@ContentLayout(R.layout.activity_healthy_programme)
class HealthyProgrammeActivity : BaseActivity() {
    val imageAdapter = ImageAdapter()
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        title_bar.title(intent.getStringExtra("title"))
        rv_healthy_programme.layoutManager = LinearLayoutManager(context)
        rv_healthy_programme.adapter = imageAdapter
        iv_healthy_programme_comment.setOnClickListener {

        }
        getData()
    }

    private fun getData() {
        lifecycleScope.request({ APIFamous.service().details(intent.getStringExtra("uuid") ?: "") },
            object : BaseAPI<FamousDetailsEntity>() {
                override fun onSuccess(t: FamousDetailsEntity?, state: Int, msg: String) {
                    tv_healthy_programme_title.text = t?.title
                    tv_healthy_programme_name.text =
                        t?.author + "\n" + t?.datetime + t?.volume + "人已读"
                    imageAdapter.setNewData(t?.details)
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

            })
    }

    class ImageAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_image_view) {
        override fun convert(helper: BaseViewHolder, item: String?) {
            glideImage(helper.getView<ResizableImageView>(R.id.resizable_image_view), item ?: "")
        }

    }

}